<?php

use Biz\model\course\Course as mCourse;
use Biz\service\course\Course as sCourse;
use Biz\service\course\CourseOrder;

class ReceiveFreeCourseAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $courseId = $this->getRequest()->getPost('course_id', 0);
        $info = mCourse::getByWhere(['course_id' => $courseId]);
        $isOwner = sCourse::isOwnerCourse($this->uid, $courseId);
        if (!$isOwner && $info && $info['is_free']) {
           $orderNO =  CourseOrder::createFreeOrderNo($courseId , $this->uid);
           $info['uid'] = $this->uid;
           $info['pay_amount'] = 0;
           $result =  CourseOrder::createOrder($this->uid ,  $orderNO, 0, $info);
           if($result){
              $this->setSuccess([
              ],self::API_CODE_SUCCES, 'm:购买成功');
           }
        } else {
            throw new \Exception('你无法领取课程', -1);
        }

    }
}